﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Reflection;

using FCNS.Utility;
using FCNS.KeepMoney.Configuration;

namespace FCNS.KeepMoney.Util
{
    class ProgramUtil
    {
        private static string m_strExePath = null;
        /// <summary>
        /// 获取程序启动路径
        /// </summary>
        /// <returns></returns>
        public static string GetExecutable()
        {
            if (m_strExePath != null) return m_strExePath;

            try { m_strExePath = Assembly.GetExecutingAssembly().Location; }
            catch (Exception) { }

            if (string.IsNullOrEmpty(m_strExePath))
            {
                m_strExePath = Assembly.GetExecutingAssembly().GetName().CodeBase;
                m_strExePath = UrlUtil.FileUrlToPath(m_strExePath);
            }

            return m_strExePath;
        }

        public static void UpdateFileOpenRecord(string lastestFile)
        {
            AceApplication configApp = Program.AppConfig.App;

            AceFileOpenRecord record = configApp.FileOpenRecords.Find(f => { return f.FilePath == lastestFile; });
            if (record != null)
                configApp.FileOpenRecords.Remove(record);
            else
            {
                record = new AceFileOpenRecord();
                record.FilePath = lastestFile;
            }

            if (configApp.FileOpenRecords.Count == 5)
                configApp.FileOpenRecords.RemoveAt(4);

            configApp.FileOpenRecords.Insert(0, record);
        }
    }
}